<?php
/**
 * Created by PhpStorm.
 * User: yancheng (cheng@love.xiaoyan.me)
 * Date: 14-9-16
 * Time: 下午3:53
 */
class Router{
    private $routeList = array();
    public function getRouteList($controllerModulePath = null){
        if(count($this->routeList) == 0){
            $this->initRouters($controllerModulePath);
        }
        return $this->routeList;
    }

    private function initRouters($controllerModulePath){
        if(!file_exists($controllerModulePath)){
            $controllerModulePath = APP_DIR.'/'.$controllerModulePath;
        }
        if(!file_exists($controllerModulePath)){
            throw new Exception("Controller modules not exist");
        }
        $files = getDirFiles($controllerModulePath,".php$");
        foreach ($files as $file) {
            $arr = include($controllerModulePath.'/'.$file);
            $name = getFileNameWithOutSuffix($file);
            $cls = new ReflectionClass($name);
            $obj = $cls->newInstance();
            $method = new ReflectionMethod($obj,'init');
            $method->invoke($obj);
        }
    }

    public static function getInstance(){

    }

    public function addRoute($url,$func){
        $this->routeList[$url] = $func;
    }
}